package com.abc.easyclaim;

import java.util.List;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKPoiInfo;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.Overlay;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class GprsMap extends MapActivity{
	
	RadioGroup mapTypeGroup;        //选择地图视图
	private BMapManager mapManager;   //地图管理者
	private MapView mapView;         //视图
	private MapController controller;
	private MKSearch mkSearch;        //定义搜索服务类
	LocationListener locationListener;     //位置动作监听器
	Bitmap ballonBitmap;
	//位置信息字符串
	String addressString;      //解析出来的具体位置字符串
	String latString;            //纬度字符串
	String longString;          //经度字符串
	
	@Override
	protected void onCreate(final Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.gprsmap);   //设置当前界面
		
		mapManager=new BMapManager(getApplication());
		mapManager.init("059FDE2924A9D6283634F113127A5E4830A6170E", null);
		mapManager.start();
		super.initMapActivity(mapManager);       //初始化百度地图
		
		mkSearch=new MKSearch();
		mkSearch.init(mapManager, new MySearchListener());   //初始化搜索服务类
		
		ballonBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ballon);  //初始化气球图片
		//获得界面上的组件
		mapView=(MapView)findViewById(R.id.mapview);
		mapTypeGroup=(RadioGroup)findViewById(R.id.rg);
		//设置显示放大、缩小的按钮
		mapView.setBuiltInZoomControls(true);
		controller=mapView.getController();
		//定义位置监听器
		locationListener=new LocationListener() {
			
			@Override
			public void onLocationChanged(final Location arg0) {
				// TODO Auto-generated method stub
				//当位置改变时获取当前经纬度
				if (arg0!=null) {
					updateMapView(arg0);	
				}
			}
			
			
		};
		//注册监听
		mapManager.getLocationManager().requestLocationUpdates(locationListener);
		
		//为RadioGroup的选中状态改变添加监听器
		mapTypeGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch (checkedId) {
				   //如果勾选的是“交通视图模式”单选按钮
				case R.id.traffic:
					mapView.setTraffic(true);
					mapView.setSatellite(false);
					break;
                   //如果勾选的是“卫星视图模式”单选按钮
				case R.id.satellite:
					mapView.setSatellite(true);
					mapView.setTraffic(false);
					break;
				}
			}
		});

	}

	//根据Location更新MapView
	private void updateMapView(Location location) {
		//将Location对象中的经纬度信息包装成GeoPoint对象
		GeoPoint gPoint=new GeoPoint((int)(location.getLatitude() * 1E6), (int)(location.getLongitude() * 1E6));
		try {
			//查询地址位置信息
			mkSearch.reverseGeocode(gPoint);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		//设置显示放大、缩小按钮
		mapView.displayZoomControls(true);
		//将地图移动到指定的地理位置
		controller.animateTo(gPoint);
		//获得MapView上原来的Overlay对象
		List<Overlay> oList=mapView.getOverlays();
		//清除原来的Overlay对象
		oList.clear();
		//添加一个新的Overlay对象
		System.out.println("addressString="+addressString);
		if (addressString!=null) {
			oList.add(new PosOverLay(gPoint,ballonBitmap,addressString));
		}
		else {
			addressString="经度："+location.getLongitude()+"\n"+"纬度："+location.getLatitude();
			oList.add(new PosOverLay(gPoint,ballonBitmap,addressString));
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		if (mapManager!=null) {
			mapManager.destroy();
			mapManager=null;
		}
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		if (mapManager!=null) {
			mapManager.stop();
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		if (mapManager!=null) {
			mapManager.start();
		}
		super.onResume();
	}
	
	public class MySearchListener implements MKSearchListener{

		/**
		 * 根据经纬度搜索地址信息结果
		 * @param arg0 搜索结果
		 * @param arg1 错误号（0表示正确返回）
		 */
		@Override
		public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
			// TODO Auto-generated method stub
			if (arg0==null) {
				return;
			}
			StringBuffer stringBuffer=new StringBuffer();
			
			//经纬度对应的位置
			stringBuffer.append(arg0.strAddr).append("\n");
			//判断该地址附近是否有POI
			if (arg0.poiList!=null) {
				//遍历所有的兴趣点信息
				//for (MKPoiInfo poiInfo : arg0.poiList) {
					//stringBuffer.delete(0, stringBuffer.length());
					//stringBuffer.append(arg0.strAddr).append("\n");
				    MKPoiInfo poiInfo=(arg0.poiList).get(0);
					stringBuffer.append("----------------").append("\n");
					stringBuffer.append("名称：").append(poiInfo.name).append("\n");
					stringBuffer.append("地址：").append(poiInfo.address).append("\n");
					stringBuffer.append("经度：").append(poiInfo.pt.getLongitudeE6()/1E6).append("\n");
					stringBuffer.append("纬度：").append(poiInfo.pt.getLatitudeE6()/1E6).append("\n");
					stringBuffer.append("电话：").append(poiInfo.phoneNum).append("\n");
					stringBuffer.append("邮编：").append(poiInfo.postCode).append("\n");
					//poi类型，0：普通点，1：公交站，2：公交线路，3：地铁站，4：地铁线路
					String poitypeString="unknow";
					switch (poiInfo.ePoiType) {
					case 0:
						poitypeString="普通点";
						break;
					case 1:
						poitypeString="公交站";
						break;
					case 2:
						poitypeString="公交线路";
						break;
					case 3:
						poitypeString="地铁站";
						break;
					case 4:
						poitypeString="地铁线路";
						break;
					}
					stringBuffer.append("类型：").append(poitypeString).append("\n");	
				//}
			}
			addressString=stringBuffer.toString();
		}

		@Override
		public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
			// TODO Auto-generated method stub
			
		}

	}

}
